package com.orion.controller;

import com.orion.domain.TestUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Administrator
 * @date 2021/7/8
 */
@RestController
@RequestMapping("/myoauth")
public class MyController {

    @GetMapping("/index")
    @PreAuthorize("#oauth2.hasScope('grgrs')")
    public String index() {
        return "index";
    }

    @GetMapping("/index1")
    @PreAuthorize("#oauth2.hasScope('user_info') and hasAnyAuthority('测试呢5')")
    public String index1() {
        return "index1";
    }

    @GetMapping("/index3")
    @PreAuthorize("#oauth2.hasScope('user_info') and hasAnyAuthority('测试呢7')")
    public String index3() {
        return "index3";
    }

    @GetMapping("/api")
    @PreAuthorize("hasAuthority('测试呢5')")
    public String api() {
        return "my api";
    }

    @GetMapping("/test6")
    @PreAuthorize("hasAuthority('测试呢6')")
    public String test6() {
        return "test6";
    }

    @GetMapping("/author1")
    @PreAuthorize("hasAuthority('author1')")
    public String author1() {
        return "author1";
    }

    @GetMapping("/any")
    @PreAuthorize("hasAnyAuthority('测试呢5','测试呢7')")
    public String any() {
        return "any";
    }


    @GetMapping("/xx")
    public String xx(Authentication authentication) {
        OAuth2Authentication o = (OAuth2Authentication) authentication;

        return authentication.toString();
    }

    @GetMapping("/user")
    public TestUser getUser(Integer id){
        TestUser testUser = new TestUser();
        testUser.setId(id);
        testUser.setBirthday(new Date());
        testUser.setAccount("testAccount");
        testUser.setEmail("123456789@qq.com");
        testUser.setTel("13045678912");
        testUser.setName("testName");
        testUser.setPassword("testPassword");
        testUser.setStatus(1);
        return testUser;
    }

    @PostMapping("/userPost")
    public TestUser getUserByPost(Integer id){
        TestUser testUser = new TestUser();
        testUser.setId(id);
        testUser.setBirthday(new Date());
        testUser.setAccount("testPostAccount");
        testUser.setEmail("123456789Post@qq.com");
        testUser.setTel("13045678912Post");
        testUser.setName("testNamePost");
        testUser.setStatus(2);
        testUser.setPassword("testPasswordPost");
        return testUser;
    }

}
